﻿<?php

class GroupApp extends BaseApp
{
    var $type = 'group';  
	
    function index()
    {
       if(!isset($_GET['gd']))
	   {
	   $_GET['gd'] ='';
	   }

        $groupid = intval($_GET['gd']);


		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		
        $this->assign('navigation', $nav->_get_nav());	

        $pic_data = include(ROOT_PATH . '/data/cache/rollpic.php'); 
		$this->assign('pic_data', $pic_data);

	 	$categorydata = & $GLOBALS['SCup']->_get_category_data('group');

        $this->assign('categorydata', $categorydata);			
			
		$TB_new = &$this->_model('default');
				
        $groupdata = $TB_new->isindex();
        $this->assign('groupdata', $groupdata);	

				
        $sellerdata = $TB_new->sellerindexlist();
        $this->assign('sellerdata', $sellerdata);					

        $link_data = include(ROOT_PATH . '/data/cache/links.php'); 
		$this->assign('link_data', $link_data);
	    
        $this->assign('view_charset', VIEW_CHARSET);
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}

        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
						
        $this->assign('site_url', SITE_URL);
				
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);		

		
        $this->display('group/groupindex.view.html');
   }


    function onegroup()
    {
       

        $groupid = intval($_GET['gd']);
      //  $sellerid = intval($_GET['sid']);

		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		
        $this->assign('navigation', $nav->_get_nav());	

	 	$categorydata = & $GLOBALS['SCup']->_get_category_data('prot');
		
        $this->assign('categorydata', $categorydata);			
		

	    $groupdata = $this->group_msg($groupid);


        $this->assign('groupdata', $groupdata);	
			
		

        $link_data = include(ROOT_PATH . '/data/cache/links.php'); 
		$this->assign('link_data', $link_data);
	    
        $this->assign('view_charset', VIEW_CHARSET);
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}
        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
						
        $this->assign('site_url', SITE_URL);
				
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);		

		
        $this->display('group/onegroup.view.html');
   }

    function sellerlist()
    {
       

		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		
        $this->assign('navigation', $nav->_get_nav());	

	 	$categorydata = & $GLOBALS['SCup']->_get_category_data('prot');
		
        $this->assign('categorydata', $categorydata);			
			
		$TB_new = &$this->_model('default');
				
        $sellerdata = $TB_new->sellerlist();							
        $this->assign('lw_data', $sellerdata['data']);		
		$this->assign('lw_page', $sellerdata['page']);	
		
        $link_data = include(ROOT_PATH . '/data/cache/links.php'); 
		$this->assign('link_data', $link_data);
	    
        $this->assign('view_charset', VIEW_CHARSET);
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}
        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
						
        $this->assign('site_url', SITE_URL);
				
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);		

		
        $this->display('group/group_brand.view.html');
   }

  
    function search()
    {
        if(!isset($_GET['sid'])){   $_GET['sid']='';}	
        if(!isset($_GET['ct'])){   $_GET['ct']='';}		   
        if(!isset($_GET['cat'])){  $_GET['cat'] ='';}		   
        if(!isset($_POST['word'])){  $_POST['word'] ='';}	
        if(!isset($_GET['v'])){  $_GET['v'] ='';}				   
        $sellerid = intval($_GET['sid']);      
		$ct = intval($_GET['ct']);
		$cat = intval($_GET['cat']);
	    $word = $_POST['word'];
		$word_array = explode(' ',$word);
		
		foreach($word_array as $key => $value)
		{
			if(!$value)
			{
             unset($word_array[$key]);
		    }  
		}

        $order = '';
		$v = $_GET['v'];
		
		if($v == 'timeasc')
		{
        $order = 'overtime asc';
		}
		else if($v == 'timedesc')
		{
        $order = 'overtime desc';		
		}

        $this->assign('ct', $ct);
        $this->assign('v', $v);
        $this->assign('cat', $cat);		 
		$TB_new = &$this->_model('default');
				
$data = $TB_new->nowgrouplist($order,$v,$word_array,$cat,$ct,$sellerid);		
        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);							
	    
        $this->assign('citydata', include(ROOT_PATH . '/data/city/city_area.php'));	  
		      
		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		
        $this->assign('navigation', $nav->_get_nav());	

	 	$category_protdata = & $GLOBALS['SCup']->_get_category_data('prot');		
        $this->assign('categoryprotdata', $category_protdata);			

	 	$category_groupdata = & $GLOBALS['SCup']->_get_category_data('group');		
        $this->assign('categorygroupdata', $category_groupdata);

        $link_data = include(ROOT_PATH . '/data/cache/links.php'); 
		$this->assign('link_data', $link_data);
	    
        $this->assign('view_charset', VIEW_CHARSET);
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}
        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
						
        $this->assign('site_url', SITE_URL);
				
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);		

		
        $this->display('group/search.view.html');
   }
  


    function ticket()
    {

		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		
        $this->assign('navigation', $nav->_get_nav());	
	
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);		
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);		 

		$this->assign('site_url',SITE_URL);

        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
		
        $this->display('group/ticket.view.html');
    }



function dockticket()
	{

        if(!isset($_POST['number'])){   $_POST['number']='';}	
        if(!isset($_POST['password'])){   $_POST['password']='';}		   
        if(!isset($_POST['submit'])){  $_POST['submit'] ='';}		   
        if(!isset($_GET['check'])){  $_GET['check'] ='';}	
        if(!isset($_GET['number'])){  $_GET['number'] ='';}	    
        if(!isset($_GET['password'])){  $_GET['password'] ='';}	
    $number = $_POST['number'];
	$password = $_POST['password'];
	$submit = $_POST['submit'];

    $check = intval($_GET['check']);

	if(!$number)$number = $_GET['number'];
	if(!$password)$password = $_GET['password'];    

	if($number=='')
	{
		$notice = &get_message(array('msg'=>'错误！','url'=>'','title'=>'Notice'));
		$notice ->display();
	}

	$sql='select * from '.DB_PREFIX.'ticket where number = \''.$number.'\'';
	$db = & get_db();
	$ticket=$db->get_one($sql);
    $msg ='';
	if($submit=='查询')
	{
		if(empty($ticket))
		{
		    $notice = &get_message(array('msg'=>'编号为'.$number.'的该优惠券不存在！','url'=>'','title'=>'Notice'));
		    $notice ->display();

		}else
		{
			if($ticket['status']==1)
			{
				$msg='该优惠券还没有被使用';
			}elseif($ticket['status']==0)
			{
				$msg='该优惠券已经被使用,使用时间是'.date('Y-m-d H:i:s',$ticket['usetime']);
			}else
			{
				$msg='该优惠券已过期';
			}

		    $notice = &get_message(array('msg'=>$msg,'url'=>'','title'=>'Notice'));
		    $notice ->display();

		}
	}else
	{
		if(empty($ticket))
		{
		    $notice = &get_message(array('msg'=>'编号为'.$number.'的该优惠券不存在！','url'=>'','title'=>'Notice'));
		    $notice ->display();

		}elseif($ticket['status']==0)
		{
			$msg='该优惠券已经被使用'.date('Y-m-d H:i:s',$ticket['usetime']);
		}elseif($ticket['status']!=1)
		{
			$msg='该优惠券已经过期';
		}
		if($msg!='')
		{
		    $notice = &get_message(array('msg'=>$msg,'url'=>'','title'=>'Notice'));
		    $notice ->display();		
		}

		$sql='select s.user_name,p.name,p.price,p.bookprice,p.nowprice,p.img from '.DB_PREFIX.'product p left join '.DB_PREFIX.'seller s on p.sellerid=s.id  where p.id = '.$ticket['productid'];
		$product=$db->get_one($sql);

		if($product['user_name']!=$GLOBALS['SCup']->SCup_user_msg['data']['user_name'])
		{
		    $notice = &get_message(array('msg'=>'只有商家才能够使用使用消费券！','url'=>'','title'=>'Notice'));
		    $notice ->display();		
		}

		if($password==$ticket['password'])
		{
			if($check==1)
			{
				$ary=array(
					'status'=>'0',
					'usetime' => time(),
				);

		        $TB_buy = &$this->_model('buy');
		        $TB_buy->ticketedit($ticket['ticketid'],$ary);

		        $notice = &get_message(array('msg'=>'消费券正确，已经成功使用！','url'=>'','title'=>'Notice','skip_time'=>'20000'));
		        $notice ->display();
			}else
			{
		        $notice = &get_message(array('msg'=>'<p>商品名称：'.$product['name'].'<br>商品原价：￥'.$product['price'].'元<br>商品团购价：￥'.$product['nowprice'].'元<br>商品预定价：￥'.$product['bookprice'].'元<br>商品图片：<img src="'.SITE_URL.'/'.UPFILE_PATH.'/'.$product['img'].'" width="180" height="230" /><br><a href="?app='.$this->type.'&act=dockticket&check=1&number='.$number.'&password='.urlencode($ticket['password']).'">该消费券存在且没有被使用！确认消费请点击这里！优惠券仅能使用一次，确认消费是不可逆操作 !</a><br><br><a href="?app='.$this->type.'&act=ticket">返回团购券查询</a></p>','url'=>'','title'=>'Notice','skip_time'=>'100000'));
		        $notice ->display();
			}
			
		}

		$notice = &get_message(array('msg'=>'编号为'.$number.'的消费券密码错误！','url'=>'','title'=>'Notice'));
		$notice ->display();

		exit;
	}
	exit;
	}








    function grouplist()
    {

		$TB_group = &$this->_model('default');
		$data = $TB_group->grouplist();
        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);

		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		
        $this->assign('navigation', $nav->_get_nav());	
	
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);		
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);		        
		$this->assign('site_url',SITE_URL);
        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
		
        $this->display('group/group_list.view.html');
    }

    function productshow()
    {

	    $id = $_GET['id'];
		$TB_new = &$this->_model('default');
				
		$data = $TB_new->group_product_seller_one($id);	

        $this->assign('data', $data);			


		$TB_pic = &$this->_model('upfile');
		$upfile_data = $TB_pic->selct($id);
        $this->assign('upfiledata', $upfile_data);		

		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}		
        $this->assign('categorydata', $categorydata);							
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);	 	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
		
        $this->display('group/productshow.view.html');
    }



    function group_msg($groupid='')
    {
        $temp_array = array();

		$TB_group = &$this->_model('default');
		$data_group = $TB_group->inindex($groupid);


        if(isset($data_group['id']))
		{
	        $temp_array = $data_group;
		    $TB_buy = &$this->_model('buy');				
		    $data_seller = $TB_buy->getsellermsg($data_group['sellerid']);	
            $temp_array['sellermsg'] = $data_seller;
			
		    $data_prot = $TB_group->productshow($order='',$word='',$data_group['id']);	
		
		   if(isset($data_prot['data']))
		   {
              foreach($data_prot['data'] as $key => $value)
		     {
	    		$data_prot['data'][$key]['agio'] = round(10/($value['price']/$value['nowprice']),1);

		     }
		   }

		  $temp_array['many_protecut'] = $data_prot;	
					
		  $data_order = $TB_buy->paytotal($data_group['id']);

		  $temp_array['paynum']=$data_order['totalorder'];
		  $temp_array['success']=$temp_array['successnum']-$temp_array['paynum'];
        }


		return $temp_array;
    }

   
}


?>